package com.example.xml;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class MainActivity extends AppCompatActivity {
private static final String TAG="tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // intiview();
        try {
            List<Person> arlist=
            XmlUtil.paramList(getAssets().open("person_list.xml"));
            for (Person data:arlist){
                Log.d(TAG, "  id  "+data.getId()+"  name  "+data.getName()+"  age  "+data.getAge());
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void intiview(){
        try {
//            获取xml数据流
            InputStream as=getAssets().open("person_list.xml");
//            创建SAX解析工具类
            Myheader myheader=new Myheader();
//            获取SAX解析器
            SAXParser parser= SAXParserFactory.newInstance().newSAXParser();
//            开始解析
            parser.parse(as,myheader);
            as.close();
           List<Person> mlist= myheader.getList();
           for (Person data:mlist){
               Log.d(TAG, "  id  "+data.getId()+"  name  "+data.getName()+"  age  "+data.getAge());
           }
        } catch (IOException | SAXException | ParserConfigurationException e) {
            e.printStackTrace();
        }
    }
}